package com.dats.util;


import java.io.*;

/**
 * @author sig
 * @since 2016/9/25
 */
public class FileUtil {

    public static byte[] readFile(String name) {
        InputStream in = null;

        try {
            in = new FileInputStream(name);
            byte[] bytes = new byte[in.available()];
            in.read(bytes);
            return bytes;
        } catch (Exception e) {
            LogUtil.console.error(e.getMessage(), e);
            return null;
        } finally {

            if (null != in) try {
                in.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
    }

    public static void writeFile(String pathName, byte[] bytes) {
        OutputStream out = null;

        try {
            File file = new File(pathName);

            if (!file.exists()) file.createNewFile();
            out = new FileOutputStream(file);
            out.write(bytes);
        } catch (Exception e) {
            LogUtil.console.error(e.getMessage(), e);
        } finally {

            if (null != out) try {
                out.close();
            } catch (IOException e) {
                LogUtil.console.error(e.getMessage(), e);
            }
        }
    }
}
